﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Imports Novacode
Imports System.IO
Imports Aspose.Words

Imports System.IO.Path
Imports System.Drawing.Printing

Partial Class Control_MSDS_ExportDocumentFile
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session("UserName") Is Nothing Then
                Response.Redirect("~/Login.aspx")
            End If

            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidMSDSID.Value = Request.QueryString("Id")
            End If
        End If

    End Sub
    Protected Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
        If Session("LanguageId") = 2 Then
            Using data As New VSEntities
                Dim p = (From q In data.MSDSOriginals Where q.Id = hidMSDSID.Value).FirstOrDefault
                If Not p Is Nothing Then
                    If Not p.OriginalFile.Contains("Upload/MSDS") Then
                        Response.Redirect(p.OriginalFile.Replace("http://ucs.com.vn/", "http://ucs.com.vn/" & "Upload/MSDS/"))
                    Else
                        Response.Redirect(p.OriginalFile)
                    End If

                End If
            End Using
        Else
            Try
                Dim FolderPath = Server.MapPath("~/Template").ToString
                Dim attributes As FileAttributes = File.GetAttributes(FolderPath & "\MSDS.docx")
                If (attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
                    attributes = attributes And Not FileAttributes.ReadOnly
                    File.SetAttributes(FolderPath & "\MSDS.docx", attributes)
                End If
                Dim LanguageId = Session("LanguageId").ToString()
                Dim FileName As String = ""
                Using document As DocX = DocX.Load(FolderPath & "\MSDS.docx")
                    Using data As New VSEntities

                        '************************* Section 1 *****************************
                        Dim p1 = (From q In data.MSDSSection01 Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value).FirstOrDefault()
                        If p1 Is Nothing Then
                            Excute_Javascript("AlertboxRedirect('MSDS chưa nhập đầy đủ dữ liệu vui lòng kiểm tra lại','../../Page/MSDS/List.aspx');", Me.Page, True)
                            Exit Sub
                        End If


                        Dim p = (From q In data.MSDSOriginals Where q.Id = hidMSDSID.Value).FirstOrDefault

                        'header
                        document.ReplaceText("<<ItemCode>>", p.ItemCode)
                        document.ReplaceText("<<ItemName>>", p.ItemName)
                        document.ReplaceText("<<VendorCode>>", p.VendorCode)
                        document.ReplaceText("<<VendorName>>", p.Vendorname)

                        document.ReplaceText("<<date>>", Now.Date.ToString("dd/MM/yyyy"))

                        document.ReplaceText("<<user name>>", Session("UserName"))


                        document.ReplaceText("<<TenChat>>", If(IsNothing(p1.S101), "", p1.S101))
                        document.ReplaceText("<<CASNo>>", p1.S104)
                        document.ReplaceText("<<ECNo>>", p1.S105)
                        document.ReplaceText("<<UNNo>>", p1.S106)


                        ''TitlePage
                        'document.ReplaceText("<<lblSection01Title>>", strlSection01Title)
                        document.ReplaceText("<<S102>>", If(IsNothing(p1.S102), "", p1.S102))
                        document.ReplaceText("<<S101>>", p1.S101)
                        document.ReplaceText("<<S103>>", p1.S103)
                        document.ReplaceText("<<S201>>", p1.S201 & " - " & p1.S203)
                        document.ReplaceText("<<S301>>", p1.S301 & " - " & p1.S303)
                        document.ReplaceText("<<S107>>", p1.S107)
                        document.ReplaceText("<<S307>>", p1.S307)
                        document.ReplaceText("<<S108>>", p1.S108)

                        '************************* End Section 1 *****************************

                     

                        '************************* Section 3*****************************
                        Dim p3 = (From q In data.MSDSSection03 Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value).FirstOrDefault()
                        If Not IsNothing(p3) Then
                            document.ReplaceText("<<S311>>", p3.S311)
                            document.ReplaceText("<<S321>>", p3.S321)
                            document.ReplaceText("<<S331>>", p3.S331)
                        Else
                            document.ReplaceText("<<S311>>", "")
                            document.ReplaceText("<<S321>>", "")
                            document.ReplaceText("<<S331>>", "")
                        End If

                        '************************* End Section 3 *****************************

                        '*************************  Section 4*****************************
                        Dim p4 = (From a In data.MSDSSection04 Where a.MSDSId = hidMSDSID.Value And a.LanguageId = LanguageId).FirstOrDefault
                        If Not IsNothing(p4) Then
                            document.ReplaceText("<<S411>>", p4.S411)
                            document.ReplaceText("<<S421>>", p4.S421)
                            document.ReplaceText("<<S431>>", p4.S431)
                            document.ReplaceText("<<S441>>", p4.S441)
                            document.ReplaceText("<<S451>>", p4.S451)
                        Else
                            document.ReplaceText("<<S411>>", "") '
                            document.ReplaceText("<<S421>>", "") 'p4.S421)
                            document.ReplaceText("<<S431>>", "") ' p4.S431)
                            document.ReplaceText("<<S441>>", "") 'p4.S441)
                            document.ReplaceText("<<S451>>", "") ' p4.S451)
                        End If


                        '************************* End Section 4 *****************************
                        '*************************  Section 5 *****************************
                        Dim p5 = (From q In data.MSDSSection05 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p5) Then
                            document.ReplaceText("<<S511>>", p5.S511)
                            document.ReplaceText("<<S521>>", p5.S521)
                            document.ReplaceText("<<S531>>", p5.S531)
                            document.ReplaceText("<<S541>>", p5.S541)
                            document.ReplaceText("<<S551>>", p5.S551)
                            document.ReplaceText("<<S561>>", p5.S561)
                        Else
                            document.ReplaceText("<<S511>>", "") ' p5.S511)
                            document.ReplaceText("<<S521>>", "") 'p5.S521)
                            document.ReplaceText("<<S531>>", "") 'p5.S531)
                            document.ReplaceText("<<S541>>", "") 'p5.S541)
                            document.ReplaceText("<<S551>>", "") 'p5.S551)
                            document.ReplaceText("<<S561>>", "") 'p5.S561)
                        End If

                        '************************* End Section 5 *****************************

                        '*************************  Section 6 *****************************
                        Dim p6 = (From q In data.MSDSSection06 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p6) Then
                            document.ReplaceText("<<S611>>", p6.S611)
                            document.ReplaceText("<<S621>>", p6.S621)
                        Else
                            document.ReplaceText("<<S611>>", "") ' p6.S611)
                            document.ReplaceText("<<S621>>", "") ' p6.S621)
                        End If

                        '************************* End Section  6 *****************************

                        '*************************  Section 7 *****************************
                        Dim p7 = (From q In data.MSDSSection07 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p7) Then
                            document.ReplaceText("<<S711>>", p7.S711)
                            document.ReplaceText("<<S721>>", p7.S721)
                        Else
                            document.ReplaceText("<<S711>>", "") ' p7.S711)
                            document.ReplaceText("<<S721>>", "") ' p7.S721)

                        End If
                        '************************* End Section  7 *****************************

                        '*************************  Section 8 *****************************
                        Dim p8 = (From q In data.MSDSSection08 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p8) Then
                            document.ReplaceText("<<S811>>", p8.S811)
                            document.ReplaceText("<<S821>>", p8.S821)
                            document.ReplaceText("<<S831>>", p8.S831)
                            document.ReplaceText("<<S841>>", p8.S841)
                        Else
                            document.ReplaceText("<<S811>>", "") ' p8.S811)
                            document.ReplaceText("<<S821>>", "") ' p8.S821)
                            document.ReplaceText("<<S831>>", "") 'p8.S831)
                            document.ReplaceText("<<S841>>", "") 'p8.S841)
                        End If
                        '************************* End Section 8 *****************************

                        '*************************  Section 9 *****************************
                        Dim p9 = (From a In data.MSDSSection09 Where a.MSDSId = hidMSDSID.Value And a.LanguageId = LanguageId).FirstOrDefault
                        If Not IsNothing(p9) Then
                            document.ReplaceText("<<S911>>", p9.S911)
                            document.ReplaceText("<<S991>>", p9.S991)

                            document.ReplaceText("<<S921>>", p9.S921)
                            document.ReplaceText("<<S9101>>", p9.S9101)

                            document.ReplaceText("<<S931>>", p9.S931)
                            document.ReplaceText("<<S9111>>", p9.S9111)

                            document.ReplaceText("<<S941>>", p9.S941)
                            document.ReplaceText("<<S9121>>", p9.S9121)

                            document.ReplaceText("<<S951>>", p9.S951)
                            document.ReplaceText("<<S9131>>", p9.S9131)

                            document.ReplaceText("<<S961>>", p9.S961)
                            document.ReplaceText("<<S9141>>", p9.S9141)

                            document.ReplaceText("<<S971>>", p9.S971)
                            document.ReplaceText("<<S9151>>", p9.S9151)

                            document.ReplaceText("<<S981>>", p9.S981)
                            document.ReplaceText("<<S9161>>", p9.S9161)

                        Else
                            document.ReplaceText("<<S911>>", "") '
                            document.ReplaceText("<<S991>>", "") 'p9.S991)

                            document.ReplaceText("<<S921>>", "") 'p9.S921)
                            document.ReplaceText("<<S9101>>", "") 'p9.S9101)

                            document.ReplaceText("<<S931>>", "") ' p9.S931)
                            document.ReplaceText("<<S9111>>", "") 'p9.S9111)

                            document.ReplaceText("<<S941>>", "") ' p9.S941)
                            document.ReplaceText("<<S9121>>", "") 'p9.S9121)

                            document.ReplaceText("<<S951>>", "") 'p9.S951)
                            document.ReplaceText("<<S9131>>", "") 'p9.S9131)

                            document.ReplaceText("<<S961>>", "") 'p9.S961)
                            document.ReplaceText("<<S9141>>", "") ' p9.S9141)

                            document.ReplaceText("<<S971>>", "") ' p9.S971)
                            document.ReplaceText("<<S9151>>", "") ' p9.S9151)

                            document.ReplaceText("<<S981>>", "") ' p9.S981)
                            document.ReplaceText("<<S9161>>", "") ' p9.S9161)

                        End If

                        '************************* End Section 9 *****************************

                        '*************************  Section 10 *****************************
                        Dim p10 = (From q In data.MSDSSection10 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p10) Then
                            document.ReplaceText("<<S1011>>", p10.S1011)
                            document.ReplaceText("<<S1021>>", p10.S1021)
                        Else
                            document.ReplaceText("<<S1011>>", "")
                            document.ReplaceText("<<S1021>>", "")
                        End If

                        ''************************* End Section 10 *****************************


                        '*************************  Section 13*****************************
                        Dim p13 = (From q In data.MSDSSection13 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p13) Then
                            document.ReplaceText("<<S1311>>", p13.S1311)
                            document.ReplaceText("<<S1321>>", p13.S1321)
                            document.ReplaceText("<<S1331>>", p13.S1331)
                            document.ReplaceText("<<S1341>>", p13.S1341)
                        Else
                            document.ReplaceText("<<S1311>>", "") '
                            document.ReplaceText("<<S1321>>", "") 'p13.S1321)
                            document.ReplaceText("<<S1331>>", "") 'p13.S1331)
                            document.ReplaceText("<<S1341>>", "") ' p13.S1341)

                        End If
                        '************************* End Section 13 *****************************

                        '*************************  Section 14 *****************************
                        Dim p14 = (From a In data.MSDSSection14 Where a.MSDSId = hidMSDSID.Value And a.LanguageId = LanguageId).FirstOrDefault
                        If Not IsNothing(p14) Then
                            document.ReplaceText("<<S1411>>", p14.S1411)
                            document.ReplaceText("<<S1412>>", p14.S1412)
                            document.ReplaceText("<<S1413>>", p14.S1413)
                            document.ReplaceText("<<S1414>>", p14.S1414)

                            document.ReplaceText("<<S1421>>", p14.S1421)
                            document.ReplaceText("<<S1422>>", p14.S1422)
                            document.ReplaceText("<<S1423>>", p14.S1423)
                            document.ReplaceText("<<S1424>>", p14.S1424)

                            document.ReplaceText("<<S1431>>", p14.S1431)
                            document.ReplaceText("<<S1432>>", p14.S1432)
                            document.ReplaceText("<<S1433>>", p14.S1433)
                            document.ReplaceText("<<S1434>>", p14.S1434)

                            document.ReplaceText("<<S1415>>", p14.S1415)
                            document.ReplaceText("<<S1416>>", p14.S1416)

                            document.ReplaceText("<<S1425>>", p14.S1425)
                            document.ReplaceText("<<S1426>>", p14.S1426)
                            document.ReplaceText("<<S1435>>", p14.S1435)
                            document.ReplaceText("<<S1436>>", p14.S1436)
                        Else
                            document.ReplaceText("<<S1411>>", "") '
                            document.ReplaceText("<<S1412>>", "") ' p14.S1412)
                            document.ReplaceText("<<S1413>>", "") ' p14.S1413)
                            document.ReplaceText("<<S1414>>", "") ' p14.S1414)

                            document.ReplaceText("<<S1421>>", "") ' p14.S1421)
                            document.ReplaceText("<<S1422>>", "") ' p14.S1422)
                            document.ReplaceText("<<S1423>>", "") ' p14.S1423)
                            document.ReplaceText("<<S1424>>", "") ' p14.S1424)

                            document.ReplaceText("<<S1431>>", "") ' p14.S1431)
                            document.ReplaceText("<<S1432>>", "") ' p14.S1432)
                            document.ReplaceText("<<S1433>>", "") ' p14.S1433)
                            document.ReplaceText("<<S1434>>", "") ' p14.S1434)

                            document.ReplaceText("<<S1415>>", "") ' p14.S1415)
                            document.ReplaceText("<<S1416>>", "") ' p14.S1416)

                            document.ReplaceText("<<S1425>>", "") ' p14.S1425)
                            document.ReplaceText("<<S1426>>", "") ' p14.S1426)
                            document.ReplaceText("<<S1435>>", "") ' p14.S1435)
                            document.ReplaceText("<<S1436>>", "") ' p14.S1436)
                        End If
                        '************************* End  Section 14 *****************************

                        '*************************  Section 15 *****************************
                        Dim p15 = (From q In data.MSDSSection15 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p15) Then
                            document.ReplaceText("<<S1511>>", p15.S1511)
                            document.ReplaceText("<<S1521>>", p15.S1521)
                        Else
                            document.ReplaceText("<<S1511>>", "")
                            document.ReplaceText("<<S1521>>", "")
                        End If

                        '************************* End Section 15 *****************************

                        '*************************  Section 16 *****************************
                        Dim p16 = (From q In data.MSDSSection16 Where q.MSDSId = hidMSDSID.Value And q.LanguageId = LanguageId).FirstOrDefault()
                        If Not IsNothing(p16) Then
                            document.ReplaceText("<<S1621>>", p16.S1621)
                            document.ReplaceText("<<S1631>>", p16.S1631)
                            document.ReplaceText("<<S1641>>", p16.S1641)
                            document.ReplaceText("<<S1661>>", p16.S1661)
                        Else
                            document.ReplaceText("<<S1621>>", "")
                            document.ReplaceText("<<S1631>>", "")
                            document.ReplaceText("<<S1641>>", "")
                            document.ReplaceText("<<S1661>>", "")
                        End If

                        '************************* End Section 16 *****************************


                        'Xuat BBTT ra file word
                        FileName = "MSDS__" & Now.ToFileTime & ".docx"
                        ' Request.Url
                        Dim URL As String = "viewerTemp/" & FileName
                        If Not Directory.Exists(Server.MapPath("~/viewerTemp").ToString) Then
                            Directory.CreateDirectory(Server.MapPath("~/viewerTemp").ToString)
                        End If
                        document.SaveAs(Server.MapPath("~/viewerTemp").ToString & "\" & FileName)
                        'xu ly convert file into pdf


                        Dim strDocFile As String = FileName.Replace(".docx", ".doc")
                        Dim strPDFFile As String = FileName.Replace(".docx", ".pdf")
                        Dim strPNPFile As String = FileName.Replace(".docx", ".pnp")
                        Dim applicationPath = Server.MapPath("~")
                        If Not File.Exists(strDocFile) Then
                            Dim licenseFile As String = Server.MapPath("~") & "\bin\Aspose.Words.lic"
                            If (File.Exists(licenseFile)) Then
                                Dim license As Aspose.Words.License = New Aspose.Words.License()
                                license.SetLicense(licenseFile)
                                Dim doc As Aspose.Words.Document = New Aspose.Words.Document(Server.MapPath("~/viewerTemp").ToString & "\" & FileName)
                                strDocFile = Server.MapPath("~/viewerTemp").ToString & "\" & strDocFile
                                strPDFFile = Server.MapPath("~/viewerTemp").ToString & "\" & strPDFFile

                                doc.Save(strDocFile, SaveFormat.Doc)
  


                                'Dim swfFile As String = GetFileNameWithoutExtension(strDocFile) & ".swf"
                                'Dim pngTemp As String = Server.MapPath("~/viewerTemp").ToString & "\" & swfFile
                                'doc.Save(pngTemp, Aspose.Words.SaveFormat.Swf)



                                'If File.Exists(pngTemp) Then
                                '    Call writeSwf("../../viewerTemp/" & swfFile)
                                'End If

                                '*************************  Section 2 *****************************
                                Dim builder As DocumentBuilder = New DocumentBuilder(doc)

                                Dim p2detail = (From q In data.Section02Detail Where q.Section02Id = hidMSDSID.Value And q.LanguageID = LanguageId).ToList()
                                If p2detail.Count > 0 Then
        
                                    Dim strHMLBeginTable_2 As String = "<table border='0' cellspacing='0' cellpadding='0' style='width:102%;border-collapse:collapse;'>"
                                    strHMLBeginTable_2 = strHMLBeginTable_2 & "<tr><td colspan='4'  style='width:100.0%;border-top:solid windowtext 2.25pt;   border-left:none;border-bottom:solid windowtext 2.25pt;border-right:none;   background:#CCCCCC;padding:0mm 5.4pt 0mm 5.4pt'><h3 align='center' style='text-align:center'><span style='font-family:Times New Roman'>PHẦN II. THÔNG TIN VỀ THÀNH PHẦN NGUY HIỂM</span></h3></td></tr>"
                                    strHMLBeginTable_2 = strHMLBeginTable_2 & "<tr style='mso-yfti-irow:1;page-break-inside:avoid'>   <td width='25%' style='width:38.32%;border-top:none;border-left:solid windowtext 1.0pt;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-bottom-alt:solid windowtext 2.25pt;   mso-border-right-alt:solid windowtext .5pt;padding:0mm 5.4pt 0mm 5.4pt'>   <h4 align='center' style='margin:0mm;margin-bottom:.0001pt;text-align:center'><span style='font-size:12.0pt'>Tên thành phần nguy hiểm</span></h4>   </td>   <td width='25%' style='width:16.14%;border-top:none;border-left:none;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;   mso-border-bottom-alt:solid windowtext 2.25pt;mso-border-right-alt:solid windowtext .5pt;   padding:0mm 5.4pt 0mm 5.4pt'>   <h4 align='center' style='margin-top:0mm;margin-right:0mm;margin-bottom:0mm;   margin-left:-5.8pt;margin-bottom:.0001pt;text-align:center'><span style='font-size:12.0pt'>Số CAS</span></h4>   </td>   <td width='25%' style='width:24.16%;border-top:none;border-left:none;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;   mso-border-bottom-alt:solid windowtext 2.25pt;mso-border-right-alt:solid windowtext .5pt;   padding:0mm 5.4pt 0mm 5.4pt'>   <p  align='center' style='text-align:center'><b>Công thức   hóa học</b></p>   </td>   <td width='25%' style='width:21.38%;border-top:none;border-left:none;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;   mso-border-bottom-alt:solid windowtext 2.25pt;mso-border-right-alt:solid windowtext .5pt;   padding:0mm 5.4pt 0mm 5.4pt'>   <p  align='center' style='text-align:center'><b>Hàm lượng(% theo trọng lượng</b>)</p>   </td>  </tr>"
                                    Dim strHTMLEndTable_2 As String = "</table>"

                                    Dim strHTML_2 As String = ""
                                    For i As Integer = 0 To p2detail.Count - 1
                                        strHTML_2 = strHTML_2 & "<tr><td align='center' valign='middle' align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p2detail(i).ThanhPhanNguyHiem & "</span></td><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p2detail(i).SoCAS & "</span></td>" & _
                                                                    "<td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p2detail(i).CongThucHoaHoc & "</span></td><td align='center' valign='middle' style='border:solid 1px #000'><span style='font-size:10pt;'>" & p2detail(i).HamLuong & "</span></td></tr>"
                                    Next
                                    builder.MoveToMergeField("Section2")
                                    builder.InsertHtml(strHMLBeginTable_2 & strHTML_2 & strHTMLEndTable_2)
                                    doc.Save(strDocFile, Aspose.Words.SaveFormat.Doc)
                                Else
                                    Dim strHMLBeginTable_2 As String = "<table border='0' cellspacing='0' cellpadding='0' style='width:102%;border-collapse:collapse;'>"
                                    strHMLBeginTable_2 = strHMLBeginTable_2 & "<tr><td colspan='4'  style='width:100.0%;border-top:solid windowtext 2.25pt;   border-left:none;border-bottom:solid windowtext 2.25pt;border-right:none;   background:#CCCCCC;padding:0mm 5.4pt 0mm 5.4pt'><h3 align='center' style='text-align:center'><span style='font-family:Times New Roman'>PHẦN II. THÔNG TIN VỀ THÀNH PHẦN NGUY HIỂM</span></h3></td></tr>"
                                    strHMLBeginTable_2 = strHMLBeginTable_2 & "<tr style='mso-yfti-irow:1;page-break-inside:avoid'>   <td width='25%' style='width:38.32%;border-top:none;border-left:solid windowtext 1.0pt;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-bottom-alt:solid windowtext 2.25pt;   mso-border-right-alt:solid windowtext .5pt;padding:0mm 5.4pt 0mm 5.4pt'>   <h4 align='center' style='margin:0mm;margin-bottom:.0001pt;text-align:center'><span style='font-size:12.0pt'>Tên thành phần nguy hiểm</span></h4>   </td>   <td width='25%' style='width:16.14%;border-top:none;border-left:none;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;   mso-border-bottom-alt:solid windowtext 2.25pt;mso-border-right-alt:solid windowtext .5pt;   padding:0mm 5.4pt 0mm 5.4pt'>   <h4 align='center' style='margin-top:0mm;margin-right:0mm;margin-bottom:0mm;   margin-left:-5.8pt;margin-bottom:.0001pt;text-align:center'><span style='font-size:12.0pt'>Số CAS</span></h4>   </td>   <td width='25%' style='width:24.16%;border-top:none;border-left:none;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;   mso-border-bottom-alt:solid windowtext 2.25pt;mso-border-right-alt:solid windowtext .5pt;   padding:0mm 5.4pt 0mm 5.4pt'>   <p  align='center' style='text-align:center'><b>Công thức   hóa học</b></p>   </td>   <td width='25%' style='width:21.38%;border-top:none;border-left:none;   border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;   mso-border-left-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;   mso-border-bottom-alt:solid windowtext 2.25pt;mso-border-right-alt:solid windowtext .5pt;   padding:0mm 5.4pt 0mm 5.4pt'>   <p  align='center' style='text-align:center'><b>Hàm lượng(% theo trọng lượng</b>)</p>   </td>  </tr>"
                                    Dim strHTMLEndTable_2 As String = "</table>"
                                    Dim strHTML_2 As String = "<tr><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td>" & _
                                                                    "<td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td style='width:22%; border:solid 1px #000'>&nbsp;</td></tr>"
                                    builder.MoveToMergeField("Section2")
                                    builder.InsertHtml(strHMLBeginTable_2 & strHTML_2 & strHTMLEndTable_2)
                                    doc.Save(strDocFile, Aspose.Words.SaveFormat.Doc)
                                End If
                                '************************* End Section 2 *****************************


                                ''*************************  Section 11 *****************************
                                Dim p11 = (From a In data.MSDSSection11 Where a.MSDSId = hidMSDSID.Value And a.LanguageId = LanguageId).FirstOrDefault
                                Dim strHMLBeginTable As String = "<table border='0' cellspacing='0' cellpadding='0' style='width:102%;border-collapse:collapse;'>"
                                strHMLBeginTable = strHMLBeginTable & "<tr><td colspan='5'  style='width:100.0%;border-top:solid windowtext 2.25pt;   border-left:none;border-bottom:solid windowtext 2.25pt;border-right:none;   background:#CCCCCC;padding:0mm 5.4pt 0mm 5.4pt'><h3 align='center' style='text-align:center'><span style='font-family:Times New Roman'>PHẦN XI. THÔNG TIN VỀ ĐỘC TÍNH</span></h3></td></tr>"
                                strHMLBeginTable = strHMLBeginTable & "<tr style='mso-yfti-irow:1;height:16.15pt'>  <td width='23%' valign='top' style='width:23.06%;border-top:none;border-left:  solid windowtext 1.0pt;border-bottom:solid windowtext 2.25pt;border-right:  solid windowtext 1.0pt;mso-border-top-alt:solid windowtext 2.25pt;mso-border-top-alt:  2.25pt;mso-border-left-alt:.5pt;mso-border-bottom-alt:2.25pt;mso-border-right-alt:  .5pt;mso-border-color-alt:windowtext;mso-border-style-alt:solid;padding:0mm 5.4pt 0mm 5.4pt;  height:16.15pt'>  <h3 align='center' style='text-align:center'><span style='font-size:10.0pt;  font-family:Times New Roman,serif'>Tên thành phần</span></h3>  </td>  <td width='19%' valign='top' style='width:19.06%;border-top:none;border-left:  none;border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;  mso-border-top-alt:solid windowtext 2.25pt;mso-border-left-alt:solid windowtext .5pt;  mso-border-top-alt:2.25pt;mso-border-left-alt:.5pt;mso-border-bottom-alt:  2.25pt;mso-border-right-alt:.5pt;mso-border-color-alt:windowtext;mso-border-style-alt:  solid;padding:0mm 5.4pt 0mm 5.4pt;height:16.15pt'>  <h3 align='center' style='text-align:center'><span style='font-size:10.0pt;  font-family:Times New Roman,serif'>Loại ngưỡng </span></h3>  </td>  <td width='17%' valign='top' style='width:17.18%;border-top:none;border-left:  none;border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;  mso-border-top-alt:solid windowtext 2.25pt;mso-border-left-alt:solid windowtext .5pt;  mso-border-top-alt:2.25pt;mso-border-left-alt:.5pt;mso-border-bottom-alt:  2.25pt;mso-border-right-alt:.5pt;mso-border-color-alt:windowtext;mso-border-style-alt:  solid;padding:0mm 5.4pt 0mm 5.4pt;height:16.15pt'>  <h3 align='center' style='text-align:center'><span style='font-size:10.0pt;  font-family:Times New Roman,serif'>Kết quả</span></h3>  </td>  <td width='22%' valign='top' style='width:22.22%;border-top:none;border-left:  none;border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;  mso-border-top-alt:solid windowtext 2.25pt;mso-border-left-alt:solid windowtext .5pt;  mso-border-top-alt:2.25pt;mso-border-left-alt:.5pt;mso-border-bottom-alt:  2.25pt;mso-border-right-alt:.5pt;mso-border-color-alt:windowtext;mso-border-style-alt:  solid;padding:0mm 5.4pt 0mm 5.4pt;height:16.15pt'>  <h3 align='center' style='text-align:center'><span style='font-size:10.0pt;  font-family:Times New Roman,serif'>Đường tiếp xúc</span></h3>  </td>  <td width='18%' valign='top' style='width:18.5%;border-top:none;border-left:  none;border-bottom:solid windowtext 2.25pt;border-right:solid windowtext 1.0pt;  mso-border-top-alt:solid windowtext 2.25pt;mso-border-left-alt:solid windowtext .5pt;  mso-border-top-alt:2.25pt;mso-border-left-alt:.5pt;mso-border-bottom-alt:  2.25pt;mso-border-right-alt:.5pt;mso-border-color-alt:windowtext;mso-border-style-alt:  solid;padding:0mm 5.4pt 0mm 5.4pt;height:16.15pt'>  <h3 align='center' style='text-align:center'><span style='font-size:10.0pt;  font-family:Times New Roman,serif'>Sinh vật thử</span></h3>  </td> </tr>"
                                Dim strHTMLEndTable As String = "</table>"
                                Dim strHTML As String = ""
                                If Not IsNothing(p11) Then
                                    '' Detail Section 111
                                    Dim p11Detail = (From q In data.Section11Detail Where q.Section11Id = p11.Section11Id).ToList()
                                    If p11Detail.Count > 0 Then
                                        For i As Integer = 0 To p11Detail.Count - 1
                                            strHTML = strHTML & "<tr><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p11Detail(i).TenThanhPhan & "</span></td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p11Detail(i).LoaiNguong & "</span></td>" & _
                                                                    "<td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p11Detail(i).KetQua & "</span></td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>" & p11Detail(i).DuongTiepXuc & "</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p11Detail(i).SinhVatThu & "</span></td></tr>"
                                        Next
                                    Else
                                        strHTML = strHTML & "<tr><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td>" & _
                                                                "<td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td></tr>"
                                    End If
                                    strHTML = strHTML & "<tr> <td colspan='5' style='width:20%; border:solid 1px #000'><div><span style='font-size:10pt;'><b>1. Các ảnh hưởng mãn tính với người: </b>" & p11.S1141 & "</span></div>" & _
                                                            "<div><span style='font-size:10pt;'><b>2. Các ảnh hưởng độc khác: </b>" & p11.S1151 & "</span></div></td></tr>"
                                Else
                                    strHTML = strHTML & "<tr><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td>" & _
                                                                                                     "<td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:20%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td></tr>"
                                    strHTML = strHTML & "<tr> <td colspan='5' style='width:20%; border:solid 1px #000'><div><span style='font-size:10pt;'><b>1. Các ảnh hưởng mãn tính với người: </b></span></div>" & _
                                                         "<div><span style='font-size:10pt;'><b>2. Các ảnh hưởng độc khác: </b></span></div></td></tr>"
                                End If
                                builder.MoveToMergeField("Section11")
                                builder.InsertHtml(strHMLBeginTable & strHTML & strHTMLEndTable)
                                doc.Save(strDocFile, Aspose.Words.SaveFormat.Doc)

                                '************************* End Section 11 *****************************




                                '*************************  Section 12 *****************************
                                Dim p12 = (From a In data.MSDSSection12 Where a.MSDSId = hidMSDSID.Value And a.LanguageId = LanguageId).FirstOrDefault
                                strHMLBeginTable = "<table border='0' cellspacing='0' cellpadding='0' style='width:102%;border-collapse:collapse;'>"
                                strHMLBeginTable = strHMLBeginTable & "<tr><td colspan='4'  style='width:100.0%;border-top:solid windowtext 2.25pt;   border-left:none;border-bottom:solid windowtext 2.25pt;border-right:none;   background:#CCCCCC;padding:0mm 5.4pt 0mm 5.4pt'><h3 align='center' style='text-align:center'><span style='font-family:Times New Roman'>PHẦN XII. THÔNG TIN VỀ SINH THÁI MÔI TRƯỜNG</span></h3></td></tr>"
                                strHTMLEndTable = "</table>"
                                strHTML = ""
                                If Not IsNothing(p12) Then
                                    strHTML = strHTML & "<tr> <td colspan='4' style='border:solid 1px #000'><div><span style='font-size:10pt;'><b>1. Độc tính với sinh vật: </b></span></div></td></tr>"
                                    '' Detail Section 111
                                    Dim p12Detail = (From q In data.Section12Detail Where q.Section12Id = p12.Section12Id).ToList()
                                    If Not IsNothing(p12Detail) Then
                                        For i As Integer = 0 To p12Detail.Count - 1
                                            strHTML = strHTML & "<tr><td align='center' valign='middle' align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p12Detail(i).TenThanhPhan & "</span></td><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p12Detail(i).LoaiSinhVat & "</span></td>" & _
                                                                        "<td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'><span style='font-size:10pt;'>" & p12Detail(i).ChukyAnhHuong & "</span></td><td align='center' valign='middle' style='border:solid 1px #000'><span style='font-size:10pt;'>" & p12Detail(i).KetQua & "</span></td></tr>"
                                        Next
                                    Else
                                        strHTML = strHTML & "<tr><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td>" & _
                                                                  "<td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td style='width:22%; border:solid 1px #000'>&nbsp;</td></tr>"
                                    End If
                                    strHTML = strHTML & "<tr> <td colspan='4' style='border:solid 1px #000'><div><span style='font-size:10pt;'><b>2. Tác động trong môi trường: </b></span></div>" & _
                                                                "<div><span style='font-size:10pt;'>" & p12.S1221 & "</span></div></td></tr>"
                                Else
                                    strHTML = strHTML & "<tr> <td colspan='4' style='border:solid 1px #000'><div><span style='font-size:10pt;'><b>1. Độc tính với sinh vật: </b></span></div></td></tr>"
                                    strHTML = strHTML & "<tr><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td>" & _
                                                              "<td align='center' valign='middle' style='width:25%; border:solid 1px #000; font-family:Times New Roman;'>&nbsp;</td><td style='width:22%; border:solid 1px #000'>&nbsp;</td></tr>"
                                    strHTML = strHTML & "<tr> <td colspan='4' style='border:solid 1px #000'><div><span style='font-size:10pt;'><b>2. Tác động trong môi trường: </b></span></div>" & _
                                                       "<div><span style='font-size:10pt;'></span></div></td></tr>"
                                End If
                                '************************* End  Section 12 *****************************
                                builder.MoveToMergeField("Section12")
                                builder.InsertHtml(strHMLBeginTable & strHTML & strHTMLEndTable)
                                doc.Save(strDocFile, Aspose.Words.SaveFormat.Doc)


                                'save pdf
                                doc = New Aspose.Words.Document(strDocFile)
                                doc.Save(strPDFFile, SaveFormat.Pdf)


                                If Not IsNothing(doc) Then
                                    doc = Nothing
                                End If



                            End If
                            Call exportFile(strPDFFile)
                        End If
                    End Using
                End Using
            Catch ex As Exception
                Excute_Javascript("Alertbox('Việc xuất dữ liệu bị lỗi! " & ex.Message & "');", Me.Page, True)
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
            End Try
        End If

    End Sub
    Private Sub writeSwf(ByVal _swf As String)
        Try
            Dim _xmlFile As String = ""
            Dim _strInfo As String = ""
            If _swf.ToLower.EndsWith(".swf") Then
                _strInfo &= " writeSwf('" & _swf & "');" & vbCrLf
            End If
            Excute_Javascript(_strInfo, Me.Page, True)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub exportFile(ByVal FileName As String)
        Try
            Dim strFileFullName As String = ""
            Dim strFileExt As String = ""
            Dim strFileContentType As String = ""
            Dim objFileInfor As FileInfo
            If File.Exists(FileName) Then
                objFileInfor = New FileInfo(FileName)
                If objFileInfor.Exists Then

                    strFileExt = Replace(LCase(objFileInfor.Extension), ".", "")
                    Select Case strFileExt
                        Case "pdf"
                            strFileContentType = "application/pdf"
                        Case "doc", "docx"
                            strFileContentType = "application/msword"
                        Case Else
                            strFileContentType = "application/x-application"
                    End Select

                    ' Clear the current output content from the buffer
                    Response.Clear()

                    ' Add the header that specifies the default filename 
                    ' for the Download/SaveAs dialog
                    Response.AddHeader("Content-Disposition", "attachment; filename=" & objFileInfor.Name)

                    ' Add the header that specifies the file size, so that the 
                    ' browser can show the download progress
                    Response.AddHeader("Content-Length", objFileInfor.Length.ToString())

                    ' Specify that the response is a stream that cannot be read _
                    ' by the client and must be downloaded
                    Response.ContentType = strFileContentType

                    ' Send the file stream to the client
                    Response.WriteFile(objFileInfor.FullName)

                    ' Release objects
                    objFileInfor = Nothing
                    'tblTemp = Nothing

                    ' Stop the execution of this page
                    Response.End()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub


End Class
